package com.dmgameclient.activities;

import java.util.concurrent.Executors;

import com.dmgameclient.R;
import com.dmgameclient.beans.Game;
import com.dmgameclient.common.BitmapUtils;
import com.dmgameclient.common.HttpUtils;
import com.dmgameclient.common.SDCardCache;
import com.dmgameclient.common.URLHelper;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @name 文章详细内容
 * @Descripation <br>
 *               1、<br>
 *               2、<br>
 * @author 樊俊彬
 * @date 2014-9-20
 * @version 1.0
 */
public class DetailGameActivity extends Activity {

	private static final String TAG = "DetailGameActivity";

	// 头部
	private Button headerHome;
	private TextView headerTitle;
	private ProgressBar headerProgress;

	// 正文
	private TextView gameName;
	private TextView gameType;
	private TextView madeCompany;
	private TextView relaseDate;
	private TextView relaseCompany;
	private TextView websit;
	private TextView terace;
	private ImageView gameLitpic;
	private WebView webView;

	private Handler handler;
	private SDCardCache sdcardCache;

	private Game game;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_game);
		this.game = (Game) getIntent().getSerializableExtra("gameObject");
		sdcardCache = new SDCardCache();
		initViews();
		loadData();
	}

	@SuppressLint("HandlerLeak")
	private void initViews() {
		// 头部
		headerHome = (Button) findViewById(R.id.headerHome);
		headerHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		headerTitle = (TextView) findViewById(R.id.headerTitle);
		headerTitle.setText("游戏详情");

		// 正文
		gameName = (TextView) findViewById(R.id.gameName);
		gameName.setText(game.getTitle());

		gameType = (TextView) findViewById(R.id.gameType);
		gameType.setText(game.getTypename());

		madeCompany = (TextView) findViewById(R.id.madeCompany);
		madeCompany.setText(game.getMade_company());

		relaseDate = (TextView) findViewById(R.id.relaseDate);
		relaseDate.setText(game.getRelease_date());

		relaseCompany = (TextView) findViewById(R.id.relaseCompany);
		relaseCompany.setText(game.getRelease_company());

		websit = (TextView) findViewById(R.id.websit);
		websit.setText(game.getWebsit());

		terace = (TextView) findViewById(R.id.terace);
		terace.setText(game.getTerrace());

		gameLitpic = (ImageView) findViewById(R.id.gameLitpic);
		webView = (WebView) findViewById(R.id.webView1);
		WebSettings settings = webView.getSettings();
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);
		webView.loadUrl(game.getArcurl());

		// 刷新界面
		handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.obj != null) {
					// 处理成指定大小显示
					byte[] data = (byte[]) msg.obj;
					gameLitpic.setImageBitmap(BitmapUtils.decodeBitmap(data,
							90, 120));
				}
			}
		};
	}

	/**
	 * 读取数据
	 */
	private void loadData() {
		Executors.newSingleThreadExecutor().execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Thread.sleep(800);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				Message msg = Message.obtain();
				String fileName = game.getLitpicFileName();

				// 优先查找本地缓存，没有时下载
				if (sdcardCache.isFileExist(fileName,
						URLHelper.CACHE_IMAGE_GAME)) {
					Log.i(TAG, ">>>本地存在>" + fileName);
					msg.obj = sdcardCache.readImageByPath(
							game.getLitpicFileName(),
							URLHelper.CACHE_IMAGE_GAME);
				} else {
					Log.i(TAG, ">>>本地不存在，开始下载>" + fileName);
					msg.obj = HttpUtils.downloadImage(URLHelper.BASE_URL
							+ game.getLitpic());
				}
				handler.sendMessage(msg);
			}
		});
	}
}
